<!-- 修改树节点的名称 -->
<template>
  <el-dialog
    :title="title"
    :visible.sync="open"
    width="560px"
    append-to-body
    @close="closeDialog"
    class="dialog"
    :close-on-click-modal="false"
  >
    <el-form :model="form" ref="queryForm" label-width="100px" size="small" :rules="rules" class="pr40">
      <el-form-item label="节点类型" prop="type" v-if="componentType === 'doc' && type === 'add'">
        <el-select v-model="form.type" placeholder="请选择节点类型" clearable style="width: 100%;">
          <el-option v-for="item in docClassifyTypes" :key="item.code" :label="item.value" :value="item.code" />
        </el-select>
      </el-form-item>
      <el-form-item label="节点名称" prop="name">
        <el-input v-model="form.name" placeholder="" clearable maxlength="200" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer pr40">
      <el-button size="small" @click="open = false">取消</el-button>
      <el-button type="primary" :loading="loading" @click="handleSubmit" size="small">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { Message } from 'element-ui'
import { getDocClassifyTypeList } from '@/api/business'

export default {
  name: 'EditTreeNodeModal',
  inject: ['componentType'],
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      default: () => ({})
    },
    type: {
      type: String,
      default: ''
    },
    handleTreeNode: {
      type: Function,
      default: () => ({})
    }
  },
  watch: {
    visible(val) {
      this.open = val
      if (val) {
        this.form.name = this.type !== 'add' ? this.nodeData?.name || '' : ''
        this.title = this.type === 'add' ? '新增分类节点' : '修改分类节点'
        if (this.componentType === 'doc' && this.type === 'add') {
          getDocClassifyTypeList().then((res) => {
            this.docClassifyTypes = res.data
          })
        }
        this.$nextTick(() => this.$refs.queryForm?.clearValidate())
      }
    }
  },
  data() {
    return {
      open: this.visible,
      form: {
        type: undefined,
        name: this.type !== 'add' ? this.nodeData?.name || '' : ''
      },
      title: this.type === 'add' ? '新增分类节点' : '修改分类节点',
      docClassifyTypes: [],
      loading: false,
      rules: {
        type: [
          { required: true, message: '请选择节点类型', trigger: 'blur' },
        ],
        name: [
          { required: true, message: '请输入名称', trigger: 'change' },
          { pattern: /\s*\S+?/, message: '名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    closeDialog() {
      this.$emit('update:visible', false)
    },
    handleSubmit() {
      this.$refs.queryForm.validate((valid) => {
        if (!valid) return
        let params = { name: this.form.name.trim(), method: 'post' }
        params.parentId = this.type === 'add' ? this.nodeData.id : this.nodeData.parentId
        params.id = this.type === 'add' ? undefined : this.nodeData.id
        if (this.componentType === 'doc') {
          params.type = this.form.type
        }
        this.loading = true
        this.handleTreeNode(params, this.type === 'add' ? 'post' : 'put')
          .then(() => {
            Message.success('保存成功')
            this.$emit('onSuccess')
            this.open = false
          })
          .finally(() => {
            this.loading = false
          })
      })
    }
  }
}
</script>
<style scoped>
.dialog,
.el-select-dropdown__item {
  font-family: 'microsoft yahei';
}
</style>
